package com.example.stuwork.controller;

import com.example.stuwork.common.Result;
import com.example.stuwork.pojo.Course;
import com.example.stuwork.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程管理控制器
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    
    /**
     * 获取所有课程
     */
    @GetMapping
    public Result<List<Course>> getAllCourses() {
        List<Course> courses = courseService.findAll();
        return Result.success("获取课程列表成功", courses);
    }
    
    /**
     * 根据ID获取课程
     */
    @GetMapping("/{courseId}")
    public Result<Course> getCourseById(@PathVariable String courseId) {
        Course course = courseService.getById(courseId);
        if (course != null) {
            return Result.success("获取课程成功", course);
        } else {
            return Result.error("课程不存在");
        }
    }
    
    /**
     * 添加课程
     */
    @PostMapping
    public Result<Course> addCourse(@RequestBody Course course) {
        if (course == null) {
            return Result.error("课程信息不能为空");
        }
        
        if (course.getCourseId() == null || course.getCourseId().trim().isEmpty()) {
            return Result.error("课程编码不能为空");
        }
        
        if (course.getCourseName() == null || course.getCourseName().trim().isEmpty()) {
            return Result.error("课程名称不能为空");
        }
        
        boolean success = courseService.addCourse(course);
        if (success) {
            Course added = courseService.getById(course.getCourseId());
            return Result.success("添加课程成功", added);
        } else {
            return Result.error("添加课程失败，课程ID或名称可能已存在");
        }
    }
    
    /**
     * 更新课程
     */
    @PutMapping("/{courseId}")
    public Result<Course> updateCourse(@PathVariable String courseId, @RequestBody Course course) {
        if (course == null) {
            return Result.error("课程信息不能为空");
        }
        
        // 确保路径中的ID与请求体一致
        course.setCourseId(courseId);
        
        boolean success = courseService.updateCourse(course);
        if (success) {
            Course updated = courseService.getById(courseId);
            return Result.success("更新课程成功", updated);
        } else {
            return Result.error("更新课程失败，课程可能不存在或名称已被使用");
        }
    }
    
    /**
     * 删除课程
     */
    @DeleteMapping("/{courseId}")
    public Result<Boolean> deleteCourse(@PathVariable String courseId) {
        boolean success = courseService.deleteCourse(courseId);
        if (success) {
            return Result.success("删除课程成功", true);
        } else {
            return Result.error("删除课程失败，课程可能不存在");
        }
    }
    
    /**
     * 搜索课程
     */
    @GetMapping("/search")
    public Result<List<Course>> searchCourses(@RequestParam(required = false) String keyword) {
        List<Course> courses = courseService.search(keyword);
        return Result.success("搜索课程成功", courses);
    }
} 